//
//  ShopBigTypeView.m
//  NewPro
//
//  Created by 何荧红 on 2018/11/3.
//  Copyright © 2018 何荧红. All rights reserved.
//

#import "ShopBigTypeView.h"

@interface ShopBigTypeView ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableview;
@property (nonatomic, strong) UITableView *tableview2;

@property (nonatomic, strong) NSArray *subArr;

@end

@implementation ShopBigTypeView

- (NSDictionary *)subinfo{
    if (!_subinfo) {
        _subinfo = @{@"餐娱类":@[@"",@"",@"",@"",@""],@"民生类":@[@"",@"",@"",@"",@""],@"公益类":@[@"",@"",@"",@"",@""],@"房产汽车类":@[@"",@"",@"",@"",@""],@"一般类":@[@"",@"",@"",@"",@""]};
    }
    return _subinfo;
}

- (instancetype)init{
    self = [super init];
    if (self) {
        self.frame = CGRectMake(0, SCREEN_HEIGHT * 2, SCREEN_WIDTH, SCREEN_HEIGHT);
        self.backgroundColor = [UIColor colorWithWhite:0 alpha:0.4];
        [self CleanWithTableView];
    }
    return self;
}

- (void)CleanWithTableView{
    self.subArr = self.subinfo[self.subinfo.allKeys[0]];
//    self.tableview = [[UITableView alloc]initWithFrame:CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH / 5 * 2, 300) style:UITableViewStylePlain];
    self.tableview = [[UITableView alloc]initWithFrame:CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, 300) style:UITableViewStylePlain];
    self.tableview.showsVerticalScrollIndicator = NO;
    self.tableview.showsHorizontalScrollIndicator = NO;
    [self.tableview registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell1"];
    self.tableview.delegate = self;
    self.tableview.dataSource = self;
    [self addSubview:self.tableview];
    
    
    self.tableview2 = [[UITableView alloc]initWithFrame:CGRectMake(SCREEN_WIDTH/5*2, SCREEN_HEIGHT, SCREEN_WIDTH/5*3, 300) style:UITableViewStylePlain];
    self.tableview2.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableview2.showsHorizontalScrollIndicator = NO;
    self.tableview2.showsVerticalScrollIndicator = NO;
    [self.tableview2 registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell2"];
    self.tableview2.delegate = self;
    self.tableview2.dataSource = self;
    self.tableview2.hidden = YES;
    [self addSubview:self.tableview2];
}
- (void)ViewDidPush{
    self.hidden = NO;
    self.frame = CGRectMake(0, -iPhoneX_Top, SCREEN_WIDTH, SCREEN_HEIGHT);
    [UIView animateWithDuration:0.3 animations:^{
        self.tableview.frame = CGRectMake(0, SCREEN_HEIGHT - 300, SCREEN_WIDTH, 300);
//        self.tableview2.frame = CGRectMake(SCREEN_WIDTH/5*2, SCREEN_HEIGHT - 300, SCREEN_WIDTH/5*3, 300);
    }];
}
- (void)ViewDidPull{
    self.hidden = YES;
    self.frame = CGRectMake(0, SCREEN_HEIGHT * 2, SCREEN_WIDTH, SCREEN_HEIGHT);
    [UIView animateWithDuration:0.3 animations:^{
        self.tableview.frame = CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, 300);
//        self.tableview2.frame = CGRectMake(SCREEN_WIDTH/5*2, SCREEN_HEIGHT, SCREEN_WIDTH/5*3, 300);
    }];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    if (tableView == self.tableview) {
        return self.subinfo.allKeys.count;
    }
    if (tableView == self.tableview2) {
        return self.subArr.count;
    }
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (tableView == self.tableview2) {
        return 45;
    }
    return 60;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.01f;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return nil;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01f;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return nil;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = nil;
    if (self.tableview == tableView) {
        UITableViewCell *DCell = [tableView dequeueReusableCellWithIdentifier:@"cell1"];
        UILabel *subtitle = [[UILabel alloc]init];
        subtitle.font = [UIFont fontWithName:@"PingFang-SC-Medium" size:16];
        subtitle.textColor = [UIColor colorWithHexString:@"333333"];
        subtitle.textAlignment = NSTextAlignmentCenter;
        if (self.subinfo.allKeys.count > 0) {
            subtitle.text = self.subinfo.allKeys[indexPath.section];
        }
        [DCell.contentView addSubview:subtitle];
        [subtitle mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(DCell.contentView.mas_left).offset(14);
            make.centerY.equalTo(DCell.contentView.mas_centerY);
            make.right.equalTo(DCell.contentView.mas_right).offset(-14);
        }];
        UIView *line = [[UIView alloc]init];
        line.backgroundColor = [UIColor colorWithHexString:@"f5f5f5"];
        [DCell.contentView addSubview:line];
        [line mas_makeConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(1);
            make.left.equalTo(DCell.contentView.mas_left).offset(14);
            make.right.equalTo(DCell.contentView.mas_right).offset(-14);
            make.bottom.equalTo(DCell.contentView.mas_bottom).offset(-1);
        }];
        cell = DCell;
    }
    if (self.tableview2 == tableView) {
        UITableViewCell *SCell = [tableView dequeueReusableCellWithIdentifier:@"cell2"];
        UILabel *subtitle = [[UILabel alloc]init];
        subtitle.font = [UIFont fontWithName:@"PingFang-SC-Medium" size:14];
        subtitle.textColor = [UIColor colorWithHexString:@"555555"];
        if (self.subArr.count > 0) {
            subtitle.text = self.subArr[indexPath.section];
        }
        [SCell.contentView addSubview:subtitle];
        [subtitle mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(SCell.contentView.mas_left).offset(4);
            make.centerY.equalTo(SCell.contentView.mas_centerY);
        }];
        UIView *line = [[UIView alloc]init];
        line.backgroundColor = [UIColor colorWithHexString:@"f5f5f5"];
        [SCell.contentView addSubview:line];
        [line mas_makeConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(1);
            make.left.equalTo(SCell.contentView.mas_left).offset(14);
            make.right.equalTo(SCell.contentView.mas_right).offset(-14);
            make.bottom.equalTo(SCell.contentView.mas_bottom).offset(-1);
        }];
        cell = SCell;
    }
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (tableView == self.tableview) {
        [self ViewDidPull];
        if (self.GetWithinfoBlock) self.GetWithinfoBlock(self.subinfo.allKeys[indexPath.section]);
    }
    if (tableView == self.tableview2) {
        self.subArr = self.subinfo[self.subinfo.allKeys[indexPath.section]];
        [self.tableview2 reloadData];
    }
}
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self ViewDidPull];
}


@end
